/* eslint-disable */
import Request from './request';
import indexConfig from '@/config/index.config';
import mHelper from '@/utils/helper';
import store from '@/store';

const http = new Request();

// request全局参数设置
http.setConfig(config => {
	/* 设置全局配置 */
	config.baseUrl = indexConfig.baseUrl; /* 根域名不同 */
	// const systemInfo = uni.getSystemInfoSync();
	// const systemInfoHeaders = {
	// 	'device-name': systemInfo.brand, // 设备名称
	// 	width: systemInfo.screenWidth, // 屏幕宽度
	// 	height: systemInfo.screenHeight, // 屏幕高度
	// 	os: systemInfo.platform, // 客户端平台
	// 	'os-version': systemInfo.system // 操作系统版本
	// };
	config.header = {
		...config.header,
	};
	return config;
});

let isRefreshing = false;
let requests = [];
http.interceptor.request(
	config => {
		/* 请求之前拦截器 */
		// if (!config.url.includes("/appNoAuth")) {
		// 	config.header['Authorization'] = uni.getStorageSync('accessToken');
		// } else {
		// 	delete config.header.Authorization
		// }
		config.header['Authorization'] = uni.getStorageSync('accessToken');
		return config;
	},
	error => {
		return Promise.reject(error);
	}
);

// 刷新refreshToken
async function handleRefreshToken(refresh_token) {
	const params = {};
	params.refresh_token = refresh_token;
	await http.post('', params).then(async r => {
		store.commit('login', r.data);
		isRefreshing = false;
	});
}
http.interceptor.response(
	async response => {
			/* 请求之后拦截器 */
			// console.log(response.data)
			switch (response.data.code) {
				case 200:
					return response.data;
				case 400:
					mHelper.toast('错误的请求');
					return Promise.reject(response.data.message);
					break;
				case 401:
					if (!store.state.isLoginPage){
						uni.removeStorageSync('accessToken');
						store.state.isLoginPage = true;
						uni.navigateTo({
							url: '/pages/login/login'
						})
						throw response.data.message;
					}
				
					// if (!store.state.refreshToken) {
					// } else {
					// 	// isRefreshing同一个页面只执行一次
					// 	if (!isRefreshing) {
					// 		isRefreshing = true;
					// 		// 刷新token
					// 		await handleRefreshToken(store.state.refreshToken, response);
					// 		requests.forEach(cb => cb());
					// 		requests = [];
					// 		isRefreshing = false;
					// 		return http.request(response.config);
					// 	} else {
					// 		return new Promise(resolve => {
					// 			// 将resolve放进队列，用一个函数形式来保存，等token刷新后直接执行
					// 			requests.push(() => {
					// 				resolve(http.request(response.config));
					// 			});
					// 		});
					// 	}
					// }
					break;
				case 405:
					mHelper.toast(response.data.message || response.data.msg);
					return Promise.reject(response.data);
				case 404:
					mHelper.toast(response.data.message || response.data.msg);
					return Promise.reject(response.data);
				case 429:
					mHelper.toast(response.data.message || response.data.msg);
					return Promise.reject(response.data);
				case 500:
					mHelper.toast(response.data.message || response.data.msg);
					return Promise.reject(response.data);
				default:
					mHelper.toast(response.data.message || response.data.msg);
					return Promise.reject(response.data);
			}
		},
		error => {
			return Promise.reject(error);
		}
);

export {
	http
};
